package net.lgy.apitest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.lgy.apitest.model.DO.ApiTestCaseDO;
import net.lgy.apitest.mapper.ApiTestCaseMapper;
import net.lgy.apitest.service.ApiTestCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * API测试用例Service实现类
 */
@Service
public class ApiTestCaseServiceImpl implements ApiTestCaseService {

    @Autowired
    private ApiTestCaseMapper apiTestCaseMapper;

    @Override
    public ApiTestCaseDO getById(Long id) {
        return apiTestCaseMapper.selectById(id);
    }

    @Override
    public boolean save(ApiTestCaseDO testCaseDO) {
        return apiTestCaseMapper.insert(testCaseDO) > 0;
    }

    @Override
    public boolean updateById(ApiTestCaseDO testCaseDO) {
        return apiTestCaseMapper.updateById(testCaseDO) > 0;
    }

    @Override
    public boolean removeById(Long id) {
        return apiTestCaseMapper.deleteById(id) > 0;
    }

    @Override
    public List<ApiTestCaseDO> getByCategoryId(Long categoryId, Long accountNo) {
        LambdaQueryWrapper<ApiTestCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCaseDO::getCategoryId, categoryId);
        queryWrapper.eq(ApiTestCaseDO::getAccountNo, accountNo);
        // 移除不存在的字段引用，使用id排序
        queryWrapper.orderByAsc(ApiTestCaseDO::getId);
        return apiTestCaseMapper.selectList(queryWrapper);
    }

    @Override
    public List<ApiTestCaseDO> getByAccountNo(Long accountNo) {
        LambdaQueryWrapper<ApiTestCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiTestCaseDO::getAccountNo, accountNo);
        // 移除不存在的字段引用，使用id排序
        queryWrapper.orderByAsc(ApiTestCaseDO::getCategoryId, ApiTestCaseDO::getId);
        return apiTestCaseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean batchRemoveByIds(List<Long> ids, Long accountNo) {
        LambdaQueryWrapper<ApiTestCaseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ApiTestCaseDO::getId, ids);
        queryWrapper.eq(ApiTestCaseDO::getAccountNo, accountNo);
        return apiTestCaseMapper.delete(queryWrapper) > 0;
    }
}